package service;

import java.util.ArrayList;
import java.util.Iterator;
import Pet.Pet;
import way.Way;

public class Service {
	private Way way = new Way();

	public ArrayList<Pet> queryPet() {
		ArrayList<Pet> data = way.queryAllData();
		return data;
	}
	
	public Pet queryData(String no){
		Pet pet = way.queryData(no);
		return pet;
	}
	public boolean addPet(String no, String type, String age, String number,
			String price) {
		ArrayList<Pet> data = queryPet();
		Iterator<Pet> iterator = data.iterator();
		while (iterator.hasNext()) {
			Pet pet = iterator.next();
			if (pet.getNo().equals(no)) {
				return false;
			}
		}
		Pet thisPet = new Pet(no, type, age, Integer.parseInt(number), Double.parseDouble(price));
		way.addPet(thisPet);
		return true;
	}

	public boolean updatePet(String no, String type,String age, String number,
			String price) {
		ArrayList<Pet> data = queryPet();
		for (int i = 0; i < data.size(); i++) {
			Pet pet = data.get(i);
			if (pet.getNo().equals(no)) {
				boolean delSuccess = way.delPet(no);
				Pet thisPet = new Pet(no, type,age,Integer.parseInt(number),
						Double.parseDouble(price));
				boolean addSuccess = way.addPet(thisPet);
				if(delSuccess && addSuccess){
					return true;
				}
			}
		}
		return false;
	}

	public boolean delPet(String no) {
		ArrayList<Pet> data = queryPet();
		for (int i = 0; i < data.size(); i++) {
			Pet pet = data.get(i);
			if (pet.getNo().equals(no)) {
				way.delPet(no);
				return true;
			}
		}
		return false;
	}

}
